package org.lanqiao.dao;

import java.util.LinkedHashMap;
import java.util.List;

import org.lanqiao.pojo.CollectPOJO;
import org.lanqiao.util.SqlUtil;

/**
 * 收藏表dao层
 * @author dzp
 *
 */
public class CollectDaoImpI {
	
	/**
	 * 1.加入收藏，根据传递的商品信息，加入收藏,判断用户是否已经把这商品加入收藏
	 */
	public int  addCollect(CollectPOJO collect) {
		String sqls = "SELECT * FROM collect c WHERE c.COLLECT_PRODUCT_ID = ? AND c.COLLECT_USER_ID = ? AND c.COLLECT_STATUS = 1  ";
		List<LinkedHashMap<String,Object>> list = SqlUtil.select3(sqls,collect.getProductId(),collect.getUserId());	
		if(list.size()>0) {
			return 1;
		}else {
			
			String sql = "INSERT INTO collect(COLLECT_ID,COLLECT_USER_ID,COLLECT_PRODUCT_ID,COLLECT_PRODUCT_PHOTO,COLLECT_PRODUCT_PRICE,COLLECT_DATE_TIME) VALUES ('1',?,?,?,?,Now())";                                       
			return SqlUtil.update(sql,collect.getUserId(),collect.getProductId(),collect.getProductPhoto(),collect.getProductPrice());

		}
		
		
	}
	
	/**
	 * 2.查询收藏列表
	 */
	public List SelectCollect(String userId) {
		String sql = "SELECT c.*,pt.*  from collect c LEFT JOIN product pt on c.COLLECT_PRODUCT_ID = pt.PRODUCT_ID WHERE c.COLLECT_USER_ID = ? AND c.COLLECT_STATUS = 1  ORDER BY c.COLLECT_DATE_TIME DESC";
		return SqlUtil.select3(sql, userId);
		
	}
	
	/**
	 * 3.删除收藏夹商品
	 */
	
	public int deleteCollect(String id) {
		String sql2 = "UPDATE  collect SET COLLECT_STATUS = 0  WHERE COLLECT_ID = ? ";
		return  SqlUtil.update(sql2, id);
		
	}

}
